package com.csht.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.csht.model.City;
import com.csht.model.CshtBrand;
import com.csht.model.CshtGoods;
import com.csht.model.CshtGoodsImages;
import com.csht.model.CshtOneMenu;
import com.csht.model.CshtThreeMenu;
import com.csht.model.CshtTwoMenu;
import com.csht.model.CshtUser;
import com.csht.model.Province;
import com.csht.model.School;
import com.csht.service.ReleaseGoodsService;
import com.csht.vo.goods.ThreeMenuVo;

/**
 * 发布商品controller
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/goods")
public class ReleaseGoodsController {
	
	/**
	 * 获取省名称链接
	 */
	private String provinceUrl = "http://119.29.166.254:9090/api/provinces";
	
	/**
	 * 通过省id查找城市
	 */
	private String cityUrl = "http://119.29.166.254:9090/api/province/getCitiesByProvinceId";
	
	/**
	 * 通过城市名获取学校
	 */
	private String schoolUrl = "http://119.29.166.254:9090/api/university/getUniversityByCityName";
	
	@Autowired
	private ReleaseGoodsService releaseGoodsService;

	/**
	 * 发布商品页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/releaseGoods")
	public String releaseGoods(ModelMap model) throws Exception{
		List<CshtOneMenu> cshtOneMenuList = releaseGoodsService.getAllOneMenu(); //获取一级菜单
		List<Province> provinceList = releaseGoodsService.getAllProvince(provinceUrl);
		model.addAttribute("cshtOneMenuList", cshtOneMenuList);
		model.addAttribute("provinceList", provinceList);
		return "/goods/releaseGoods";
	}
	
	/**
	 * 根据一级菜单查询二级菜单和品牌
	 */
	@RequestMapping(value = "/getTowMenuAndBrandByOneMenuId")
	@ResponseBody
	public Map<String, Object> getTowMenuAndBrandByOneMenuId(@RequestParam(name = "oneMenuId") Integer oneMenuId) throws Exception {
		Map<String,Object> res = new HashMap<String,Object>();
		List<CshtTwoMenu> cshtTwoMenuList = releaseGoodsService.getTowMenuByOneMenuId(oneMenuId);
		List<CshtBrand> cshtBrandList = releaseGoodsService.getBrandByOneMenuId(oneMenuId);
		res.put("cshtBrandList", cshtBrandList);
		res.put("cshtTwoMenuList", cshtTwoMenuList);
		return res;
	}
	
	/**
	 * 根据二级菜单id查询三级菜单
	 */
	@RequestMapping(value = "/getThreeMenuByTwoMenuId")
	@ResponseBody
	public Map<String,List<CshtThreeMenu>> getThreeMenuByTwoMenuId(@RequestParam(name = "twoMenuId") Integer twoMenuId) throws Exception {
		Map<String,List<CshtThreeMenu>> res = new HashMap<String,List<CshtThreeMenu>>();
		List<CshtThreeMenu> cshtThreeMenuList = releaseGoodsService.getThreeMenuByTwoMenuId(twoMenuId);
		res.put("cshtThreeMenuList", cshtThreeMenuList);
		return res;
	}
	
	/**
	 * 根据省id获取该省全部城市
	 */
	@RequestMapping(value = "/getAllCityByProvinceId")
	@ResponseBody
	public Map<String,List<City>> getAllCityByProvinceId(@RequestParam(name = "provinceId") Integer provinceId) throws Exception {
		Map<String,List<City>> res = new HashMap<String,List<City>>();
		List<City> cityList = releaseGoodsService.getAllCityByProvinceId(cityUrl, provinceId);
		res.put("cityList", cityList);
		return res;
	}

	/**
	 * 根据城市名获取学校
	 */
	@RequestMapping(value = "/getAllSchoolByCityName")
	@ResponseBody
	public Map<String,List<School>> getAllSchoolByCityName(@RequestParam(name = "cityName") String cityName) throws Exception {
		Map<String,List<School>> res = new HashMap<String,List<School>>();
		List<School> schoolList = releaseGoodsService.getAllSchoolByCityName(schoolUrl, cityName);
		res.put("schoolList", schoolList);
		return res;
	}
	
	/**
	 * 搜索类目
	 * @param threeMenuName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMenuToSearch")
	@ResponseBody
	public Map<String, List<ThreeMenuVo>> getMenuToSearch(@RequestParam(name = "threeMenuName") String threeMenuName) throws Exception{
		Map<String, List<ThreeMenuVo>> res = new HashMap<String, List<ThreeMenuVo>>();
		List<ThreeMenuVo> threeMenuVoList = releaseGoodsService.getMenuToSearch("%" + threeMenuName + "%");
		res.put("threeMenuVoList", threeMenuVoList);
		return res;
	}
	
	/**
	 * 添加商品记录
	 * @param model
	 * @param cshtGoods
	 * @param request
	 * @param multipartFile
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/insertGoods")
	public String insertGoods(ModelMap model, CshtGoods cshtGoods, HttpServletRequest request, 
			@RequestParam(name = "goodsimg") MultipartFile[] multipartFile) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取用户id
		cshtGoods.setUserId(cshtUser.getUserId()); //设置用户id
		releaseGoodsService.insertGoods(cshtGoods); //插入商品记录
		//上传商品图片
		String originalFilename = "";
		String offixIndex = "";
		String imgPath = "";
		CshtGoodsImages cshtGoodsImages = new CshtGoodsImages();
		File file = null;
		for(int i = 0; i < multipartFile.length; i++) {
			if(multipartFile[i].getName() == "" || multipartFile[i].getName() == null) {
				continue;
			}
			originalFilename = multipartFile[i].getOriginalFilename(); //获取原文件名
			offixIndex = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length() - 1); //获取后缀名
			imgPath = System.currentTimeMillis() + i + offixIndex; //文件路径
			file = new File("D:\\Tomcat\\csht_file\\goods_images\\" + imgPath); //创建文件
			multipartFile[i].transferTo(file); //保存文件
			cshtGoodsImages.setGoodsId(cshtGoods.getGoodsId());
			cshtGoodsImages.setGoodsImagesPath(imgPath);
			releaseGoodsService.insertGoodsImages(cshtGoodsImages); //插入商品图记录
			//判断用户设置的是哪一张图为商品封面图
			if(cshtGoods.getGoodsImgpath().equals((i + 1) + "")) {
				cshtGoods.setGoodsImgpath(imgPath);
				releaseGoodsService.updateGoodsImapath(cshtGoods); //更新商品封面图
			}
		}
		
		return "/goods/rgoodsSuccess";
	}
	
}
